/*
*
* NMaker
*
* This class takes the data extracted from KMLReader and creates a
* NHood object for each row
*
*/
class NMaker {
  
  // attributes
  String[] nameArray, nnumArray, descArray, coordStringArray;
  String[][] censusDataArray, otherDataArray;
  
  // ----------------------------------------
  // constructor method
  // ----------------------------------------
  NMaker(String[][] fromKML, String[][] fromCensus, String[][] fromOther) {
    
   nameArray = fromKML[0];
   nnumArray = fromKML[1];
   descArray = fromKML[2];
   coordStringArray = fromKML[3];
   censusDataArray = fromCensus;   
   otherDataArray = fromOther;
    
  } // constructor method
  
  // -----------------------------------------
  // makeNHoods
  // this method creates an array of NHood objects for output
  // -----------------------------------------
  NHood[] makeNHoods() {
  
    // initiate output array
    NHood[] out = new NHood[(nameArray.length)];
    
    // iterate through rows of data and create NHood object for each
    for (int i = 0; i < nameArray.length; i++) {
       
      // split entry from coordinate strings array on colon
      // this will only result in more than item if the neighbour hood
      // in question is composed of more than one non-contiguous polygon
      String[] coordStrings = split(coordStringArray[i], ":");
      ArrayList coordOut = new ArrayList();
      // iterate through the list of polygons
      for(int j = 0; j < coordStrings.length; j++) {
        
        // split the string of coordinates for each polygon into
        // individual 3-element coordinate lines
        String[] coordStringsArray = split(coordStrings[j], ' ');
        
        PVector[] coords = new PVector[coordStringsArray.length - 1];
        PVector ll = new PVector();
        
        // iterate through each line
        for (int k = 0; k < (coordStringsArray.length - 1); k++) { // note: use coordStringsArray.length - 1 b/c last coord has trailing space
          // split each line into individual elements
          String[] h = split(coordStringsArray[k], ',');
          
          // convert each element to UTM coordinates and store
          ll.set( float(h[0]), float(h[1]), 0);
          coords[k] = utmConverter.latLongToUTM(ll);
        } // for loop
        
        // add coordinate matrix to output ArrayList
        coordOut.add(coords);
      } // for loop
      
      // create NHood object
      out[i] = new NHood(trim(nameArray[i]).toUpperCase(), descArray[i], nnumArray[i], coordOut);
      
      // search censusDataArray for neighbourhood's census data and import to NHood object
      for(int m = 0; m < censusDataArray.length; m++) {
        
        // compare neighbourhood name with name in censusDataArray; if the same, import data
        if(out[i].name.equals(censusDataArray[m][0])) {
          
          out[i].importCensusData(censusDataArray[m]);
          break;
        } // if
        
        // some "neighbourhoods" in KML file don't have census data; call below
        // method for these neighbourhoods
        // (ALSO: is there a better way to code this then below? The whole m == (censusDataArray.length - 1)
        // thing seems a bit clumsy, but I'm really tired right now and don't feel like trying to fix it).
        if(m == (censusDataArray.length - 1)) { 
          out[i].importNullCensusData();
        } // if
        
      } // for
      
      // search otherDataArray for other neighbourhood data and import to NHood object
      for(int m = 0; m < otherDataArray.length; m++) {
        
        // compare neighbourhood name with name in censusDataArray; if the same, import data
        if(out[i].name.equals(otherDataArray[m][0])) {
          
          out[i].importOtherData(otherDataArray[m]);
          break;
        } // if
        
        // some "neighbourhoods" in KML file don't have census data; call below
        // method for these neighbourhoods
        // (ALSO: is there a better way to code this then below? The whole m == (censusDataArray.length - 1)
        // thing seems a bit clumsy, but I'm really tired right now and don't feel like trying to fix it).
        if(m == (otherDataArray.length - 1)) { 
          out[i].importNullOtherData();
        } // if
        
      } // for
      
      // make metric hash
      out[i].makeHash();
      
    } // for 
    
  return out;
  
  } // makeNHoods method
  
} // class
